package com.syf.core.engine;

import com.syf.core.config.SyApplicationConfiguration;
import com.syf.core.kit.ToolKitProvider;
import com.syf.core.service.invoke.SyServiceInvoker;

import java.util.Map;

/**
 * 书昀框架上下文对象，由它对外提供框架各类功能。
 */
public class SyFrameContext {
    private SyApplicationConfiguration configuration;

    private ToolKitProvider toolKitProvider;

    private SyServiceInvoker serviceInvoker;

    public SyFrameContext() {

    }

    /**
     * 获取 tool kit
     */
    public <T> T getToolKit(Class<T> toolKitClazz) {
        if (toolKitProvider == null) {
            throw new RuntimeException("错误代码，toolKitProvider还未初始化！");
        }

        return toolKitProvider.getToolKit(toolKitClazz);
    }

    /**
     * 获取service执行器
     */
    public SyServiceInvoker getServiceInvoker() {
        if (serviceInvoker == null) {
            throw new RuntimeException("错误代码，serviceInvoker还未初始化！");
        }

        return serviceInvoker;
    }

    /**
     * 注入SyApplicationConfiguration
     */
    void insertSyApplicationConfiguration(SyApplicationConfiguration configuration) {
        if (configuration == null && this.configuration != null) {
            throw new RuntimeException("已存在SyApplicationConfiguration，请核实！");
        }

        this.configuration = configuration;
    }

    /**
     * 注入ToolKitProvider
     */
    void insertToolKitProvider(ToolKitProvider toolKitProvider) {
        if (toolKitProvider == null && this.toolKitProvider != null) {
            throw new RuntimeException("已存在ToolKitProvider，请核实！");
        }

        this.toolKitProvider = toolKitProvider;
    }

    /**
     * 注入service执行器
     */
    void insertServiceInvoker(SyServiceInvoker serviceInvoker) {
        if (serviceInvoker == null && this.serviceInvoker != null) {
            throw new RuntimeException("已存在SyfServiceInvoker，请核实！");
        }

        this.serviceInvoker = serviceInvoker;
    }

    /**
     * 获取配置参数
     */
    public String getProperty(String propertyName) {
        return configuration.getPropertyFromApplication(propertyName);
    }

}
